package my.algorithms.mcorrea.topcoder;

import java.util.Arrays;

/**
 *Your company has just undergone some software upgrades, and you will now be storing all of the
 * names of your clients in a new database. Unfortunately, your existing data is 
 * inconsistent, and cannot be imported as it is. You have been tasked with writing a 
 * program to cleanse the data.
 * 
 * You are given a String[], names, which is a list of the names of all of your existing 
 * clients. Some of the names are in "First Last" format, while the rest are in "Last, First" format.
 * You are to return a String[] with all of the names in "First Last" format, sorted by last name,
 * then by first name.
 * @author mcorrea
 */
public class ClientList {

    public static void main(String[] args) {
		String[] names = {"Joe Smith", "Brown, Sam", "Miller, Judi"};
		print(dataCleanup(names));
		
		String[] names2 = {"Kelly, Anthony", "Kelly Anthony", "Thompson, Jack"};
		print(dataCleanup(names2));
		
		String[] names3 = {"Campbell, Phil", "John Campbell", "Young, Warren"};
		print(dataCleanup(names3));
    }
	
	public static String[] dataCleanup(String[] names){
		
		for(int k=0;k<names.length;k++){
			String name = names[k];
			if(name.indexOf(",")>=0){
				String[] temp = name.split(", ");
				names[k] = temp[0]+" "+temp[1];
			}else{
				String[] temp = name.split("\\s");
				names[k] = temp[1]+" "+temp[0];
			}
		}
		
		//sorting
		Arrays.sort(names);
		
		for(int k=0;k<names.length;k++){
			String name = names[k];
			String[] temp = name.split("\\s");
			names[k] = temp[1]+" "+temp[0];
		}
		
		return names;
	
	}
	
	private static void print(String[] names){
		for(String s: names){
			System.out.print(s+" - ");
		}
		System.out.println();
	}
	

}
